package com.gzk.hrm.web.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.gzk.hrm.domain.Login;
import com.gzk.hrm.dto.LoginMealDto;
import com.gzk.hrm.query.LoginQuery;
import com.gzk.hrm.service.ILoginService;
import com.gzk.hrm.util.AjaxResult;
import com.gzk.hrm.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    public ILoginService loginService;

    @RequestMapping(value = "/user",method = RequestMethod.POST)
    public AjaxResult login(@RequestBody @Valid Login login){
        return AjaxResult.me().setResultObj(loginService.login(login));
    }
    @RequestMapping("/loginSuccess")
    public String loginSuccess(){
        return "登錄成功";
    }

    /**
    * 保存和修改公用的
    * @param login  传递的实体
    * @return Ajaxresult转换结果
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public AjaxResult save(@RequestBody @Valid Login login){
        if(login.getId()!=null){
            loginService.updateById(login);
        }else{
            loginService.insert(login);
            System.out.println(login.getId());
        }
        return AjaxResult.me().setResultObj(login.getId());
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            loginService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public Login get(@PathVariable("id")Long id)
    {
        return loginService.selectById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public List<Login> list(){

        return loginService.selectList(null);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public PageList<Login> json(@RequestBody LoginQuery query)
    {
        Page<Login> page = new Page<Login>(query.getPage(),query.getRows());
        page = loginService.selectPage(page);
        return new PageList<Login>(page.getTotal(),page.getRecords());
    }


    /**
     * 保存login和meal中间表
     */
    @RequestMapping(value = "/saveLoginMeal",method = RequestMethod.POST)
    public AjaxResult saveLoginMeal(@RequestBody LoginMealDto dto){
        loginService.saveLoginMeal(dto);
        return AjaxResult.me();

    }
}
